import { createRouter, createWebHistory } from 'vue-router';
import { useUserStore } from '@/stores/userStore.js';
import { ElMessage } from 'element-plus';
import component from 'element-plus/es/components/tree-select/src/tree-select-option.mjs';

const routes = [
    {
        path: '/',
        redirect: '/Auth/Login'
    },
    {
        path: '/Admin',
        redirect: '/Admin/Desktop'
    },
    {
        path: '/User',
        redirect: '/User/Desktop'
    },
    {
        path: '/Auth',  // 登录验证页面
        component: () => import('@/views/authentication/Auth.vue'),
        children: [
            {
                path: 'Login',
                name: 'Login',
                component: () => import('@/views/authentication/Login.vue')
            },
            {
                path: 'Register',
                name: 'Register',
                component: () => import('@/views/authentication/Register.vue')
            }
        ]
    },
    {
        path: '/Admin', // 管理员页面
        component: () => import('@/views/main-page/MainPage.vue'),
        children: [
            {
                path: 'Desktop',
                name: 'Desktop',
                component: () => import('@/views/main-page/admin/Desktop.vue')
            },
            {
                path: 'UserInfo',
                name: 'userInfo',
                component: () => import('@/views/main-page/admin/userInfo.vue')
            },
            {
                path: 'AwardInfo',
                name: 'AwardInfo',
                component: () => import('@/views/main-page/admin/awardInfo.vue')
            }
        ]
    },
    {
        path: '/User', // 用户页面
        component: () => import('@/views/main-page/MainPage.vue'),
        children: [

        ]
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes
});

// // 添加全局前置守卫
// router.beforeEach((to, from, next) => {
//     const userStore = useUserStore();
//     const isAuthenticated = userStore.isLoggedIn;
//     const isAdmin = userStore.isAdmin;

//     if (to.path.startsWith('/Admin') && !isAuthenticated) {
//         ElMessage.error('登录信息失效，请重新登陆');
//         next('/Auth/Login');
//     } else if (to.path.startsWith('/User') && !isAuthenticated) {
//         ElMessage.error('登录信息失效，请重新登陆');
//         next('/Auth/Login');
//     } else if (to.path === '/' && isAuthenticated) {
//         if (isAdmin) {
//             next('/Admin/Desktop');
//         } else {
//             next('/User/Desktop');
//         }
//     } else {
//         next();
//     }
// });

export default router;